<?php
function parseCommand($params) {
	if(file_exists($params[0]) && !is_dir($params[0])) {
		$file = file_get_contents($params[0]);
		$file = explode("\n",$file);
		$file = str_replace("\t","",$file);
		foreach($file as $line) {
			$cmd = explode(';',$line);
			$cmd = $cmd[0]; //Tot ce e dupa ; este comment, si va fi ignorat
			$cmd = trim($cmd);

			// Avem linia de comanda si dorim sa scoatem parametrii
			// in asa fel incat sa se tina cont de stringurile introduse intre 
			// ghilimele (deci versiunea explode cu separator ' ' nu va merge
			$params = array(); 
			$k = 0;				// Contor pentru indexul array-ului de parametri
			$ins = false; 		// Daca suntem sau nu in interiorul unui sir de caractere
			for($i=0; $i< strlen($cmd); $i++) {
				if($cmd[$i]=='"') $ins = !$ins;				// Negam variabila cand se deschid si inchid sirurile
				if(($ins==false)&&($cmd[$i]==' ')) $k++;	// Daca nu suntem in sir si gasim spatiu putem trece la urmatorul element
				if($cmd[$i]!='"') {
					$params[$k] .= $cmd[$i];				// Adaugam caracterul la parametru
				}
			}
			foreach($params as &$param) { $param = trim($param); }	// Eliminam spatiile din margine
			unset($param);
			 
			//var_dump($params);
			
			if(!empty($params[0])) {
				processParams($params);
			}
			//var_dump($cmd[1]);
			unset($cmd); 
			unset($params);
		}
		//var_dump($file);
	} else {
		processParams($params);
	}
	
}


function processParams($params) {

	//if(file_exists($params[0])) { echo "File Parser"; return; }	// Process file

	$command = array_shift( $params );
	if(strpos($params[0],'/') === 0) {
		$flag = array_shift( $params );
	} else $flag = null;
	
	$data = array();
	$type = null;
	
	switch($command) {
		case 'mkapp':
			if(is_null($flag)) { $type = TK_APP_TMPL; }
			elseif($flag == "/e") { $type = TK_APP_EMPTY; }
			else { 
				echo "Ignoring unknown flag ".$flag." on mkapp command; type \"help\" for information\n";
				$type = TK_APP_TMPL;
			}
			
			if(empty($params[2])) {
				echo "> !!! You need to specify at least 3 params for the makeapp command\n";
				return false;
			}
			 
			$data['name'] 	= $params[0];
			$data['prefix'] = $params[1];

			if($type == TK_APP_TMPL) {
				$data['app_tmpl'] = $params[2];
				$data['tmpl'] = @$params[3];
				$data['lang'] = $params[4] ? $params[4] : 'en_GB';	// Limba este necesara pentru fisiere de limba;
				$data['copy'] = @$params[5];
				$data['desc'] = @$params[6];
				$data['keyw'] = @$params[7];
			} elseif($type == TK_APP_EMPTY) {
				$data['tmpl'] = @$params[2];
				$data['lang'] = @$params[3];
				$data['copy'] = @$params[4];
				$data['desc'] = @$params[5];
				$data['keyw'] = @$params[6];
			}
			
			if(!is_null( $type )) {
				Toolkit::tkCreate($data,$type);
			} else {
				echo "> !!! Undefined type for".$data['name'].";\n";
				return false;
			}
			
			break;
		
		case 'mkload':
			if(empty($params[1])) {
				echo "> !!! mkload needs two parameters! \n";
				return false;
			}

			$data['app'] = $params[0];
			$data['prefix'] = $params[1];
			
			$type = TK_ITEM_LOADER;
			
			Toolkit::tkCreate($data, $type);

			break;
			
		case 'new':
			if(is_null($flag)) {
				echo "> !!! You need to specify the type of file you wish to create; use \"help\" for information\n";
				return false;
			}
			
			switch($flag) {
				case '/cv':	$type = TK_ITEM_CONTROLLER_VIEW; break;
				case '/m': $type = TK_ITEM_MODEL; break;
				case '/t': $type = TK_ITEM_TABLE; break;
				case '/mod': $type = TK_ITEM_MODULE; break;
				default : $type = null;  
			}
			if(!empty( $params[0] ) && !empty( $params[1] )) {
				$data['name'] 	= $params[0];
				$data['app'] 	= $params[1];
				$data['prefix']	= @$params[2];
			} else {
				echo "> !!! You need to specify at least two params\n";
				return false;
			}
			
			if(!is_null( $type )) {
				Toolkit::tkCreate($data, $type);
			} else {
				echo "> !!! Undefined type;\n";
				return false;
			}
			
			break;
			
		case 'help':
			$helpFile = './help/help_cli';
			if(!empty($params[0])) {
				$helpFile .= '_'.$params[0];
			}
			if(file_exists($helpFile)) {
				$content = file_get_contents($helpFile);
				echo "\n--------------------------------------------------\n";
				echo $content;
				echo "\n--------------------------------------------------\n"; 
			} else echo "Couldn't find help file $helpFile !\n";
			
			break;
	}

}